package com.cssw.fyzb.busi.api.acct;

import com.cssw.fyzb.busi.convert.Converter;
import com.cssw.fyzb.core.model.dodb.AcctType;
import com.cssw.fyzb.core.model.vo.acct.AcctRecordVo;
import com.cssw.fyzb.busi.service.acct.AcctService;
import com.cssw.fyzb.core.model.eos.AcctLogEo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhouxu
 * @create 2020-12-18 17:12
 */
@Function(description = "账本日志查询", parameters = {
        @Parameter(name = "page", type = Type.Integer, description = "当前页", required = true),
        @Parameter(name = "limit", type = Type.Integer, description = "页大小", required = true),
        @Parameter(name = "userId", type = Type.String, description = "用户ID", required = true),
        @Parameter(name = "acctType", type = Type.String, description = "账本类型(F Y G)", required = true),
        @Parameter(name = "feeInDe", type = Type.String, description = "加减（1加 2减）", required = false),
})
@Component
public class ListAcctLog implements Action {

    private static final Logger logger = LoggerFactory.getLogger(ListAcctLog.class);

    @Autowired
    private AcctService acctService;

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        String acctType = request.getString("acctType");
        Integer page = request.getInteger("page");
        Integer limit = request.getInteger("limit");
        Page<AcctLogEo> logPage = acctService.queryAcctLog(userId, acctType, page - 1, limit);
        List<AcctRecordVo> voList = ((Converter<List<AcctLogEo>, List<AcctRecordVo>>) tmpList -> {
            List<AcctRecordVo> vos = new ArrayList<>();
            tmpList.forEach((v) -> {
                AcctRecordVo vo = new AcctRecordVo();
                vo.setAcctType(v.getAcctType());
                if (AcctType.ACCT_TYPE_G.equals(acctType)) {
                    vo.setFee(v.getFee().setScale(7, BigDecimal.ROUND_HALF_UP));
                }else{
                    vo.setFee(v.getFee().setScale(2, BigDecimal.ROUND_HALF_UP));
                }
                vo.setUserId(v.getUserId());
                vo.setAcctId(v.getAcctId());
                vo.setFeeInDee(v.getFeeInDe());
                vo.setTradeId(v.getTradeId());
                vo.setTradeType(v.getTradeType());
                vo.setTradeName(v.getTradeName());
                vo.setBusiType(v.getBusiType());
                vo.setCreateTime(v.getCreateTime());
                vos.add(vo);
            });
            return vos;
        }).convert(logPage.getList());

        return Response.success().put("list", voList).put("total", logPage.getTotalRow()).put("count", logPage.getTotalPage());
    }
}
